using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.Sql;
using System.Data.SqlClient;

/// <summary>
/// Summary description for PetType
/// </summary>
public class PetTypeDAO : DBConnection
{
    #region sql command
    string SQL_SELECT_ID = "";
    string SQL_SELECT_ALL = "";
    string SQL_INSERT = "insert into TypeOfPet(CategoryID, KindName, Original, Info) values(@categoryId, @kindName, @original, @info)";
    string SQL_UPDATE = "update TypeOfPet set CategoryID = @categoryId, KindName = @kindName, Original = @original, Info = @info where TypeID = @typeId";
    string SQL_DELETE = "delete from TypeOfPet where TypeID = @typeId";
    #endregion

    #region fields
    string message = "";
    #endregion

    #region constructors
    public PetTypeDAO()
    {
    }
    #endregion

    #region properties
    public string Message
    {
        get { return message; }      
    }
    #endregion

    #region methods
    public int InsertPetType(PetType petType)
    {
        int effectedRow = 0;
        try
        {
            SqlCommand cmd = new SqlCommand(SQL_INSERT, conn);
            cmd.Parameters.AddWithValue("@categoryId", petType.Category.ID);
            cmd.Parameters.AddWithValue("@kindName", petType.Name);
            cmd.Parameters.AddWithValue("@original", petType.Origin);
            cmd.Parameters.AddWithValue("@info", petType.Information);
            if (Connect())
            {
                effectedRow = cmd.ExecuteNonQuery();
                Disconnect();
            }
        }
        catch (SqlException ex) { message = ex.Message; }
        catch (Exception ex) { message = ex.Message; }
        return effectedRow;
    }

    public int UpdatePetType(PetType petType)
    {
        int effectedRow = 0;
        try
        {
            SqlCommand cmd = new SqlCommand(SQL_UPDATE, conn);
            cmd.Parameters.AddWithValue("@typeId", petType.ID);
            cmd.Parameters.AddWithValue("@categoryId", petType.Category.ID);
            cmd.Parameters.AddWithValue("@kindName", petType.Name);
            cmd.Parameters.AddWithValue("@original", petType.Origin);
            cmd.Parameters.AddWithValue("@info", petType.Information);
            if (Connect())
            {
                effectedRow = cmd.ExecuteNonQuery();
                Disconnect();
            }
        }
        catch (SqlException ex) { message = ex.Message; }
        catch (Exception ex) { message = ex.Message; }
        return effectedRow;
    }

    public int DeletePetType(PetType petType)
    {
        int effectedRow = 0;
        try
        {
            SqlCommand cmd = new SqlCommand(SQL_DELETE, conn);
            cmd.Parameters.AddWithValue("@typeId", petType.ID);
            if (Connect())
            {
                effectedRow = cmd.ExecuteNonQuery();
                Disconnect();
            }
        }
        catch (SqlException ex) { message = ex.Message; }
        catch (Exception ex) { message = ex.Message; }
        return effectedRow;
    }
    #endregion
}
